﻿using System;
using System.Collections;
using Portables.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Navigation;
using WindowsStoreApp.Model;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace WindowsStoreApp
{
   /// <summary>
   ///    An empty page that can be used on its own or navigated to within a Frame.
   /// </summary>
   public sealed partial class MainPage
   {
      public static readonly DependencyProperty MakeColumnProperty = DependencyProperty.Register("MakeColumn", typeof(int), typeof(MainPage), new PropertyMetadata(0));
      public static readonly DependencyProperty ModelColumnProperty = DependencyProperty.Register("ModelColumn", typeof(int), typeof(MainPage), new PropertyMetadata(1));

      private readonly ObservableKeyedCollection<string, Automobile> _items;
      private readonly ObservableView<Automobile> _view;

      public MainPage()
      {
         _items = new ObservableKeyedCollection<string, Automobile>(a => a.Model);
         _view = new ObservableView<Automobile>(_items, (x, y) => String.Compare(x.Model, y.Model, StringComparison.Ordinal));

         _items.Add(new Automobile("Ford", "Taurus"));
         _items.Add(new Automobile("Honda", "Accord"));

         InitializeComponent();
      }

      public IList Items
      {
         get { return _items; }
      }

      public ObservableView<Automobile> View
      {
         get { return _view; }
      }

      public int MakeColumn
      {
         get { return (int)GetValue(MakeColumnProperty); }
         set { SetValue(MakeColumnProperty, value); }
      }

      public int ModelColumn
      {
         get { return (int)GetValue(ModelColumnProperty); }
         set { SetValue(ModelColumnProperty, value); }
      }

      /// <summary>
      ///    Invoked when this page is about to be displayed in a Frame.
      /// </summary>
      /// <param name="e">
      ///    Event data that describes how this page was reached.  The Parameter
      ///    property is typically used to configure the page.
      /// </param>
      protected override void OnNavigatedTo(NavigationEventArgs e)
      {
         
      }

      private void OnClearButtonClick(object sender, RoutedEventArgs e)
      {
         _items.Clear();
      }

      private void OnSortMakeButtonClick(object sender, RoutedEventArgs e)
      {
         MakeColumn = 0;
         ModelColumn = 1;
         _view.Sort = new DelegatedComparer<Automobile>((x, y) => String.Compare(x.Make, y.Make, StringComparison.Ordinal));
      }

      private void OnSortModelButtonClick(object sender, RoutedEventArgs e)
      {
         MakeColumn = 1;
         ModelColumn = 0;
         _view.Sort = new DelegatedComparer<Automobile>((x, y) => String.Compare(x.Model, y.Model, StringComparison.Ordinal));
      }

      private void OnFilterButtonClick(object sender, RoutedEventArgs e)
      {
         string filter = txtFilter.Text;
         _view.Filter = string.IsNullOrWhiteSpace(filter) ? (Predicate<Automobile>)null : (a => a.Make.ToLower().Contains(filter.ToLower()) || a.Model.ToLower().Contains(filter.ToLower()));
      }

      private void OnAddButtonClick(object sender, RoutedEventArgs e)
      {
         string make = txtMake.Text;
         string model = txtModel.Text;
         Automobile auto = new Automobile(make, model);

         int index;
         if(!int.TryParse(txtIndex.Text, out index))
         {
            _items.Add(auto);
         }
         else
         {
            _items.Insert(index, auto);   
         }
      }
   }
}